﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ChatApp.Core.Framework;
using ChatApp.Core.Info;
using NHibernate;

namespace ChatApp.Core.Business
{
    class UserRoleBusiness : IUserRoleBusiness
    {
        private ISessionFactory aISessionFactor;

        private ISession aISession;

        private ITransaction aTransaction = null;
        public UserRoleBusiness()
        {
            aISessionFactor = NHibernateSessionFactory.InstanceSessionFactory();
        }

        public void Insert(UserRoleInfo pUserRoleInfo)
        {

            try
            {
                aISession = aISessionFactor.OpenSession();
                aTransaction = aISession.BeginTransaction();

                aISession.Save(pUserRoleInfo);
                aTransaction.Commit();
            }
            catch (Exception ex)
            {
                aTransaction.Rollback();
                throw new Exception(ex.Message);
            }
            finally
            {
                if (aISession.IsOpen)
                {
                    aISession.Close();
                }
            }

        }

        public void Update(UserRoleInfo pUserRoleInfo)
        {

            try
            {
                aISession = aISessionFactor.OpenSession();
                aTransaction = aISession.BeginTransaction();

                aISession.Update(pUserRoleInfo);
                aTransaction.Commit();
            }
            catch (Exception ex)
            {
                aTransaction.Rollback();
                throw new Exception(ex.Message);
            }
            finally
            {
                if (aISession.IsOpen)
                {
                    aISession.Close();
                }
            }

        }

        public void Delete(UserRoleInfo pUserRoleInfo)
        {

            try
            {
                aISession = aISessionFactor.OpenSession();
                aTransaction = aISession.BeginTransaction();

                aISession.Delete(pUserRoleInfo);
                aTransaction.Commit();
            }
            catch (Exception ex)
            {

                aTransaction.Rollback();
                throw new Exception(ex.Message);
            }
            finally
            {
                if (aISession.IsOpen)
                {
                    aISession.Close();
                }
            }

        }

        public IList<UserRoleInfo> SelectAll()
        {

            try
            {
                aISession = aISessionFactor.OpenSession();

                return aISession.QueryOver<UserRoleInfo>().List();
            }
            catch (Exception ex)
            {

                aTransaction.Rollback();
                throw new Exception(ex.Message);
            }
            finally
            {
                if (aISession.IsOpen)
                {
                    aISession.Close();
                }
            }

        }


        public UserRoleInfo SelectByPrimaryId(string pId)
        {

            try
            {
                aISession = aISessionFactor.OpenSession();

                return aISession.Get<UserRoleInfo>(pId);
            }
            catch (Exception ex)
            {
                aTransaction.Rollback();
                throw new Exception(ex.Message);
            }
            finally
            {
                if (aISession.IsOpen)
                {
                    aISession.Close();
                }
            }

        }

        public IList<UserRoleInfo> SelectByParams(IList<ParameterInfo> pParams)
        {

            try
            {

                aISession = aISessionFactor.OpenSession();

                string aHibernateQuery = "from UserRoleInfo A where 1 = 1 ";

                foreach (var aParams in pParams)
                {
                    aHibernateQuery += "and A." + aParams.Name + " = :" + aParams.Name + " ";
                }

                var aIQuery = aISession.CreateQuery(aHibernateQuery);
                foreach (var aParams in pParams)
                {
                    aIQuery.SetParameter(aParams.Name, aParams.Value);
                }

                return aIQuery.List<UserRoleInfo>();
            }
            catch (Exception ex)
            {
                aTransaction.Rollback();
                throw new Exception(ex.Message);
            }
            finally
            {
                if (aISession.IsOpen)
                {
                    aISession.Close();
                }
            }

        }
    }
}
